package com.vil.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vil.common.ServerResponse;
import com.vil.entity.Admin;
import com.vil.entity.Charge;
import com.vil.entity.User;
import com.vil.mapper.UserMapper;
import com.vil.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public ServerResponse insertSelective(User record) {
        record.setJoin_time(new Date());

        try {
            int i = 0;
            i = userMapper.insertSelective(record);
            if (i>0){
                return ServerResponse.buildSuccessMsg("添加成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ServerResponse.buildErrorMsg("添加失败");
    }

    @Override
    public ServerResponse deleteByPrimaryKey(Long id) {
        if (id==null){
            return ServerResponse.buildErrorMsg("参数不能为空！");
        }
        int i = userMapper.deleteByPrimaryKey(id);
        if (i>0){
            return ServerResponse.buildSuccessMsg("删除成功");
        }
        return ServerResponse.buildErrorMsg("删除失败");
    }

    @Override
    public ServerResponse updateByPrimaryKeySelective(User record) {
        int i = 0;
        try {
            i = userMapper.updateByPrimaryKeySelective(record);
            if (i>0){
                return ServerResponse.buildSuccessMsg("修改成功");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return ServerResponse.buildErrorMsg("修改失败");
    }

    @Override
    public ServerResponse getListByManyConditions(Integer pageIndex, Integer pageSize, User user) {
        PageHelper.startPage(pageIndex, pageSize);

        try {
            List<User> list = null;
            list = userMapper.getListByManyConditions(user);
            PageInfo pageInfo = new PageInfo(list);
            if (pageInfo!=null){
                return ServerResponse.buildSuccessData(pageInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServerResponse.buildErrorMsg("列表不存在");
    }


    @Override
    public ServerResponse deleteMany(Long[] ids) {

        try {
            if (ids==null){
                return ServerResponse.buildErrorMsg("数组不能为空");
            }
            int i = 0;
            i = userMapper.deleteMany(ids);
            if (i>0){
                return ServerResponse.buildSuccessMsg("删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServerResponse.buildErrorMsg("删除失败");
    }

    @Override
    public ServerResponse getById(Long id) {
        User user = null;
        try {
             user = userMapper.selectByPrimaryKey(id);
            if (user != null) {
                return ServerResponse.buildErrorMsg("不存在该管理员信息");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServerResponse.buildSuccessData(user);
    }
}

